package io.sdl.modules.sys.controller;

import io.sdl.common.annotation.LogOperation;
import io.sdl.common.constant.Constant;
import io.sdl.common.page.PageData;
import io.sdl.common.utils.ExcelUtils;
import io.sdl.common.utils.Result;
import io.sdl.common.validator.AssertUtils;
import io.sdl.common.validator.ValidatorUtils;
import io.sdl.common.validator.group.AddGroup;
import io.sdl.common.validator.group.DefaultGroup;
import io.sdl.common.validator.group.UpdateGroup;
import io.sdl.modules.security.user.SecurityUser;
import io.sdl.modules.security.user.UserDetail;
import io.sdl.common.sys.dto.SdlInstitutionDTO;
import io.sdl.modules.sys.excel.SdlInstitutionExcel;
import io.sdl.common.sys.service.SdlInstitutionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@RestController
@RequestMapping("sdl/institution")
@Tag(name = "")
public class SdlInstitutionController {
    @Autowired
    private SdlInstitutionService sdlInstitutionService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("sdl:institution:page")
    public Result<PageData<SdlInstitutionDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {

        PageData<SdlInstitutionDTO> page = sdlInstitutionService.page(params);
        return new Result<PageData<SdlInstitutionDTO>>().ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    @RequiresPermissions("sdl:institution:list")
    public Result<List<SdlInstitutionDTO>> list() {
        List<SdlInstitutionDTO> list = sdlInstitutionService.list(new HashMap<>(1));

        return new Result<List<SdlInstitutionDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("sdl:institution:info")
    public Result<SdlInstitutionDTO> get(@PathVariable("id") Long id) {
        SdlInstitutionDTO data = sdlInstitutionService.getbyId(id);

        return new Result<SdlInstitutionDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("sdl:institution:save")
    public Result save(@RequestBody SdlInstitutionDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);


        sdlInstitutionService.addOrganization(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("sdl:institution:update")
    public Result update(@RequestBody SdlInstitutionDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        UserDetail user = SecurityUser.getUser();
        dto.setUpdateTime(DateTime.now().toDate());
        dto.setUpdater(user.getId());
        sdlInstitutionService.updateFromId(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("sdl:institution:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sdlInstitutionService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("sdl:institution:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SdlInstitutionDTO> list = sdlInstitutionService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "", list, SdlInstitutionExcel.class);
    }

}
